#!/bin/bash

# Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2018  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Merged some ideas from Alex Sarmardzic's script for qt4 at SlackBuilds.org
# ^^ Modified by Robby Workman <rworkman@slackware.com> for QT4 & KDE4

# Modifications 2010, 2011, 2012  Eric Hameleers, Eindhoven, NL

# qt-4.8.7 is built from The Qt Company source, with lots of patches
# from various sources.

# This is the SBo-ized version. Heinz Wiesinger (pprkut) got this
# to build on Slackware-current as of January 23, 2021. The stuff
# in patches/ was collected (and in a few cases written) by him,
# and he worked out the correct order to apply them. B. Watson
# (yalhcru@gmail.com) modified pprkut's version to make it fit the SBo
# template, and to make it install to /usr/lib(64)?/qt4/ to stay out of
# the way.

# 20220409 bkw: BUILD=2
# - forgot to put the SlackBuild in the doc dir.
# - resize icon correctly so it's really 48x48.
# - get rid of Encoding from .desktop files (deprecated).

# 20220302 bkw: webkit already wasn't being built, even with the
# -webkit option passed to configure, so update the script to remove
# the option to disable it (it's always disabled), and update the
# README to document that fact.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=qt4
VERSION=${VERSION:-4.8.7}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "armv7hl" ]; then
  # To prevent "qatomic_armv6.h error: output number 2 not directly addressable"
  # More permanent solution is to patch gcc:
  # http://bazaar.launchpad.net/~linaro-toolchain-dev/gcc-linaro/4.6/revision/106731
  SLKCFLAGS="-O2 -march=armv7-a -mfpu=vfpv3-d16 -fno-strict-volatile-bitfields"
  LIBDIRSUFFIX=""
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

case "$ARCH" in
    arm*) TARGET=$ARCH-slackware-linux-gnueabi ;;
    *)    TARGET=$ARCH-slackware-linux ;;
esac

set -e

SRC=qt-everywhere-opensource-src-$VERSION

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRC
tar xvf $CWD/$SRC.tar.gz
cd $SRC

chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

if [ $ARCH = "i486" -o $ARCH = "i586" -o "$ARCH" = "i686" ]; then
  sed -i -e "s/QMAKE_CFLAGS_RELEASE        += -O2/QMAKE_CFLAGS_RELEASE        += $SLKCFLAGS/" mkspecs/common/gcc-base.conf
fi

# 20220123 bkw: reworked a couple of these so they apply with -p1.
# The funky sed stuff and while loop is so the 'series' file can
# have comments and blank lines.
sed -n -e 's/#.*//' -e '/./p' $CWD/patches/series | while read diff; do
  echo "=== Applying $diff"
  patch -p1 < $CWD/patches/$diff
done

export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS -std=gnu++98 -fpermissive -fno-delete-null-pointer-checks"
export OPENSOURCE_CXXFLAGS="$CXXFLAGS"

./configure \
  -confirm-license \
  -opensource \
  -prefix /usr/lib${LIBDIRSUFFIX}/qt4 \
  -fast \
  -release \
  -system-libpng \
  -system-libjpeg \
  -system-zlib \
  -system-sqlite \
  -plugin-sql-sqlite \
  -dbus \
  -no-webkit \
  -no-phonon \
  -nomake examples \
  -nomake demos \
  -nomake docs \
  -no-separate-debug-info \
  -no-pch \
  -xmlpatterns
  # No-precompiled-headers is ccache-friendly.

make
make install INSTALL_ROOT=$PKG

# Binaries are already stripped.

# Don't ship .la files:
rm -f $PKG/usr/lib${LIBDIRSUFFIX}/qt4/lib/*.la

# 20220124 bkw: don't think we still need this, since this is no longer
# a core Slackware package.
# The infamous qt -> qt-${VERSION} link that keeps the full path out of .la files:
#( cd $PKG/usr/lib${LIBDIRSUFFIX} ; ln -sf qt qt-${VERSION} )

# Add a missing Qt.pc which is needed by KDE applications:
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/qt4/lib/pkgconfig
cat <<EOF > $PKG/usr/lib${LIBDIRSUFFIX}/qt4/lib/pkgconfig/Qt.pc
prefix=/usr/lib${LIBDIRSUFFIX}/qt4
bindir=\${prefix}/bin
datadir=\${prefix}
docdir=\${prefix}/doc
headerdir=\${prefix}/include
importdir=\${prefix}/imports
libdir=\${prefix}/lib
moc=\${bindir}/moc
plugindir=\${prefix}/plugins
qmake=\${bindir}/qmake
sysconfdir=\${prefix}/etc/settings
translationdir=\${prefix}/translations

Name: Qt
Description: Qt Configuration
Version: $VERSION
EOF

# libjscore is used internally.  Prevent a false dependency on this in the .la and .pc files:
sed -i "s,-ljscore,,g" $PKG/usr/lib${LIBDIRSUFFIX}/qt4/lib/pkgconfig/*.pc
sed -i "s,-L../JavaScriptCore/release,,g" $PKG/usr/lib${LIBDIRSUFFIX}/qt4/lib/pkgconfig/*.pc

# Make sure that .la, .pc, and .prl files use the correct $LIBDIRSUFFIX:
sed -i "s,-L/usr/X11R6/lib ,-L/usr/X11R6/lib${LIBDIRSUFFIX} ,g" \
  $PKG/usr/lib${LIBDIRSUFFIX}/qt4/lib/pkgconfig/*.pc \
  $PKG/usr/lib${LIBDIRSUFFIX}/qt4/lib/*.prl

# Link the shared libraries into /usr/lib:
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  for file in qt4/lib/*.so* ; do
    ln -sf $file .
  done
)

# Add profile scripts
mkdir -p $PKG/etc/profile.d
sed -e "s#usr/lib/#usr/lib${LIBDIRSUFFIX}/#g" $CWD/profile.d/qt4.sh \
  > $PKG/etc/profile.d/qt4.sh
sed -e "s#usr/lib/#usr/lib${LIBDIRSUFFIX}/#g" $CWD/profile.d/qt4.csh \
  > $PKG/etc/profile.d/qt4.csh
chmod 0644 $PKG/etc/profile.d/*  # do not make executable by default.

# Put a ton of links to more "normal" places.  I'd just use a prefix of /usr, but it
# creates a ton of new (and ambiguously named) /usr directories...
mkdir -p $PKG/usr/bin
( cd $PKG/usr/lib${LIBDIRSUFFIX}/qt4/bin
  for file in * ; do
    ( cd $PKG/usr/bin ; ln -sf /usr/lib${LIBDIRSUFFIX}/qt4/bin/$file $file-qt4 )
  done
)

mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig
( cd $PKG/usr/lib${LIBDIRSUFFIX}/qt4/lib/pkgconfig
  for file in *.pc ; do
    ( cd $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig ; ln -sf /usr/lib${LIBDIRSUFFIX}/qt4/lib/pkgconfig/$file . )
  done
)

# Install Qt's private headers - at least Gentoo and Fedora are adding these
# and some software has (inevitably) started depending on them:
# We're using `rsync -R` as easy way to preserve relative path names:
rsync -aR \
  include/Qt{Core,Declarative,Gui,Script}/private \
  src/{corelib,declarative,gui,script}/*/*_p.h \
  ${PKG}/usr/lib${LIBDIRSUFFIX}/qt4/

# Add menu entries for all those hidden but great Qt applications:
# Assistant icons
install -p -m644 -D tools/assistant/tools/assistant/images/assistant.png $PKG/usr/share/icons/hicolor/32x32/apps/assistant.png
install -p -m644 -D tools/assistant/tools/assistant/images/assistant-128.png $PKG/usr/share/icons/hicolor/128x128/apps/assistant.png
# Designer icon
install -p -m644 -D tools/designer/src/designer/images/designer.png $PKG/usr/share/icons/hicolor/128x128/apps/designer.png
# Linguist icons
for icon in tools/linguist/linguist/images/icons/linguist-*-32.png ; do
  size=$(echo $(basename ${icon}) | cut -d- -f2)
  install -p -m644 -D ${icon} $PKG/usr/share/icons/hicolor/${size}x${size}/apps/linguist.png
done
# Qt logo:
convert doc/src/images/qt-logo.png -resize 48x48! $PKG/usr/share/icons/hicolor/48x48/apps/qt-logo.png

# And the .desktop files
mkdir -p $PKG/usr/share/applications
cat <<EOF > $PKG/usr/share/applications/designer.desktop
[Desktop Entry]
Name=Qt4 Designer
GenericName=Interface Designer
Comment=Design GUIs for Qt4 applications
Exec=designer
Icon=designer
MimeType=application/x-designer;
Terminal=false
Type=Application
Categories=Qt;Development;
EOF
cat <<EOF > $PKG/usr/share/applications/assistant.desktop
[Desktop Entry]
Name=Qt4 Assistant
Comment=Shows Qt4 documentation and examples
Exec=assistant
Icon=assistant
Terminal=false
Type=Application
Categories=Qt;Development;Documentation;
EOF
cat <<EOF > $PKG/usr/share/applications/linguist.desktop
[Desktop Entry]
Name=Qt4 Linguist
Comment=Add translations to Qt4 applications
Exec=linguist
Icon=linguist
MimeType=text/vnd.trolltech.linguist;application/x-linguist;
Terminal=false
Type=Application
Categories=Qt;Development;
EOF
cat <<EOF > $PKG/usr/share/applications/qtconfig.desktop
[Desktop Entry]
Name=Qt4 Config
Comment=Configure Qt4 behavior, styles, fonts
Exec=qtconfig
Icon=qt-logo
Terminal=false
Type=Application
Categories=Qt;Settings;
EOF

# Add a documentation directory:
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a *GPL_EXCEPTION* LICENSE* README* changes-* $PKGDOC
if [ -d $PKG/usr/lib${LIBDIRSUFFIX}/qt4/doc/html ]; then
  ( cd $PKGDOC
    ln -sf /usr/lib${LIBDIRSUFFIX}/qt4/doc/html .
  )
fi
cat $CWD/README_SBo.txt > $PKGDOC/README_SBo.txt
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
